package com.catsic.user.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import com.catsic.base.dao.impl.JdbcBaseDaoImpl;
import com.catsic.user.TestUser;
import com.catsic.user.dao.JdbcUserDao;

@Repository("jdbcUserDao")
public class JdbcUserDaoImpl extends JdbcBaseDaoImpl<TestUser, String> implements
		JdbcUserDao {

	@Override
	public TestUser get(String id) {
		String sql = "SELECT ID, USERNAME, REALNAME FROM TEST_USER WHERE ID = ?";  
		TestUser testUser = this.jdbcTemplate.queryForObject(sql, new Object[] {id}, BeanPropertyRowMapper.newInstance(TestUser.class));
        return testUser;
	}

	@Override
	public void add(TestUser user) {
		String sql = "INSERT INTO TEST_USER(ID, USERNAME, REALNAME) VALUES(:id, :username, :realname)";  
		SqlParameterSource ps = new BeanPropertySqlParameterSource(user);  
        namedParameterJdbcTemplate.update(sql, ps);
	}

	@Override
	public void delete(String id) {
		this.jdbcTemplate.update("DELETE FROM TEST_USER WHERE ID = ?",id);
	}

	@Override
	public void update(TestUser user) {
		String sql = "UPDATE TEST_USER SET REALNAME = :realname WHERE ID = :id"; 
		SqlParameterSource ps = new BeanPropertySqlParameterSource(user);  
        namedParameterJdbcTemplate.update(sql, ps);
	}

	@Override
	public Integer getTotalCount() {
		String sql = "SELECT COUNT(*) FROM TEST_USER";
		Integer count = this.jdbcTemplate.queryForObject(sql, Integer.class);
		return count;
	}

	@Override
	public List<TestUser> getList(int startIndex, int count) {
		return null;
	}

	@Override
	public List<TestUser> getAll() {
		List<TestUser> users = null;
		return users;
	}

}
